// pages/login/login.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "18381500979",
    verification: "",
    rightVerification: "",
    active: 1,
    icon: {
      normal: 'https://rzzt-1317097558.cos.ap-chengdu.myqcloud.com/%E7%99%BB%E9%99%86%E7%8A%B6%E6%80%81.png',
      active: 'https://rzzt-1317097558.cos.ap-chengdu.myqcloud.com/%E6%9C%AA%E7%99%BB%E5%BD%95%E7%8A%B6%E6%80%81.png',
    },
    count: 60,
    toGetCode: false
  },


  /**
   * 生命周期函数--监听页面加载
   */

  onLoad(options) {

  },



  onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({ active: event.detail });
  },
  handlePhone(e) {
    if (!/^1\d{10}$/.test(e.detail.value)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000
      })
      return
    } else {
      this.setData({
        phone: e.detail.value
      })
    }
  },
  handleSendCode(e) {
    if (!/^\d{6}$/.test(e.detail.value)) {
      wx.showToast({
        title: '验证码格式不正确',
        icon: 'none',
        duration: 2000
      })
      return
    } else {
      this.setData({
        verification: e.detail.value
      })
    }
  },

  getCode() {
    // console.log(1323)
    if (this.data.toGetCode) {
      return
    }
    if (!/^1\d{10}$/.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000
      })
      return
    } else {
      this.setData({
        toGetCode: true
      })
      let count = 60
      let timer = setInterval(() => {
        count--
        this.setData({
          count: count
        })
        if (count === 0) {
          clearInterval(timer)
          this.setData({
            count: 60,
            toGetCode: false
          })
        }
      }, 1000)
      // 将手机号转为字符串
      let phone = this.data.phone.toString()
      wx.$http.get('/code', { mobile: phone }).then(res => {
        console.log(res)
        if (res.data.code === 10000) {
          wx.showToast({
            title: '验证码发送成功',
            icon: 'none',
            duration: 2000
          })
          this.setData({
            rightVerification: +res.data.data.code
          })
        } else {
          wx.showToast({
            title: '验证码发送失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },
  async toLogin() {
    if (!/^1\d{10}$/.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!/^\d{6}$/.test(this.data.verification)) {
      wx.showToast({
        title: '验证码格式不正确',
        icon: 'none',
        duration: 2000
      })
      return
    }
    // console.log(this.data.verification)
    // console.log(this.data.rightVerification)
    if (this.data.verification != this.data.rightVerification) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none',
        duration: 2000
      })
      return
    }
    // const token = wx.getStorageSync('token')
    // if (!token) {
    // 向后端发送请求 获取token
    let { data: { data: { token, refreshToken } } } = await wx.$http.post('/login', { mobile: this.data.phone.toString(), code: this.data.verification.toString() })
    console.log(token)
    console.log(refreshToken)
    // 将token存入缓存
    wx.setStorageSync('token', token)
    wx.setStorageSync('refreshToken', refreshToken)
    // } else {
    //   // token存在并且过期后的处理
    //   // 删除token
    //   console.log('token存在')
    //   wx.removeStorageSync('token')
    //   let { data } = await wx.$http.post('/refreshToken')
    //   console.log(data)
    // }



    wx.switchTab({
      url: '/pages/index/index',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },





  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})